package com.google.api.chart;

import static com.google.api.chart.encoding.JoinHelper.array;
import static com.google.api.chart.encoding.JoinHelper.filter;
import static com.google.api.chart.encoding.JoinHelper.join;
import static com.google.api.chart.encoding.JoinHelper.notNullFilter;

import java.util.ArrayList;
import java.util.List;

import com.google.api.chart.features.AxisAndLabelFeature;
import com.google.api.chart.features.GridLineFeature;
import com.google.api.chart.features.RangeMarkerFeature;
import com.google.api.chart.features.ShapeMarkerFeature;

public final class AxisGridShapeRangeFeatureHelper<T> implements AxisAndLabelFeature<T>, GridLineFeature<T>, ShapeMarkerFeature<T>, RangeMarkerFeature<T>, ChartTypeMaker {

	private T outer;
	private AxisMaker[] axes = null;
	private String chm = null;
	private String chg = null;
	private String chm2 = null;
	private String chm3 = null;

	protected AxisGridShapeRangeFeatureHelper(T outer) {
		this.outer = outer;
	}
	
	@Override
	public T axes(AxisMaker... axes) {
		this.axes   = axes;
		return outer;
	}
	
	@Override
	public T gridlines(GridLineMaker gridlines) {
		this.chg  = gridlines.toString();
		return outer;
	}
	
	@Override
	public T dataLabels(DataLabelMaker... dataLabels) {
		this.chm = join(dataLabels, "|");
		return outer;
	}
	
	@Override
	public T shapes(ShapeMarkerMaker... shapes) {
		this.chm2 = join(shapes, "|");
		return outer;
	}
	
	@Override
	public T ranges(RangeMarkerMaker... ranges) {
		this.chm3 = join(ranges, "|");
		return outer;
	}
	
	@Override
	public void fillParameters(List<String> parameters) {
		// build axises
		buildAxises(parameters);
		// data labels
		if (chm != null)
			parameters.add("chm=" + chm);
		// gridlines
		if (chg != null)
			parameters.add("chg=" + chg);
		// shape markers
		if (chm2 != null)
			parameters.add("chm=" + chm2);
		if (chm3 != null)
			parameters.add("chm=" + chm3);
	}

	private void buildAxises(List<String> parameters) {
		
		if (axes == null)
			return;
		
		List<String> chxt = new ArrayList<String>();
		List<String> chxl = new ArrayList<String>();
		List<String> chxp = new ArrayList<String>();
		List<String> chxr = new ArrayList<String>();
		List<String> chxs = new ArrayList<String>();
		List<String> chxtc = new ArrayList<String>();
		
		int axisIdx = 0;
		for (AxisMaker axis : axes) {

			chxt.add(axis.toString());
			chxl.add(axis.labelToString(axisIdx));
			chxp.add(axis.positionToString(axisIdx));
			chxr.add(axis.rangeToString(axisIdx));
			chxs.add(axis.stylesToString(axisIdx));
			chxtc.add(axis.stylesTickmarkToString(axisIdx));
			
			axisIdx++;
		}
		
		String[] chxtA = filter(array(chxt, String.class), String.class, notNullFilter);
		if (chxtA.length > 0)
			parameters.add("chxt="+join(chxtA, ","));

		String[] chxlA = filter(array(chxl, String.class), String.class, notNullFilter);
		if (chxlA.length > 0)
			parameters.add("chxl="+join(chxlA, "|"));

		String[] chxpA = filter(array(chxp, String.class), String.class, notNullFilter);
		if (chxpA.length > 0)
			parameters.add("chxp="+join(chxpA, "|"));

		String[] chxrA = filter(array(chxr, String.class), String.class, notNullFilter);
		if (chxrA.length > 0)
			parameters.add("chxr="+join(chxrA, "|"));

		String[] chxsA = filter(array(chxs, String.class), String.class, notNullFilter);
		if (chxsA.length > 0)
			parameters.add("chxs="+join(chxsA, "|"));

		String[] chxtcA = filter(array(chxtc, String.class), String.class, notNullFilter);
		if (chxtcA.length > 0)
			parameters.add("chxtc="+join(chxtcA, "|"));
		
	}
	

}
